<?php

define('DBCOON', 'mongodb://root:root@192.168.0.108/dict/');
define('DICTDB','dict');
define('COLLEC','dictionary');
/*
	字典在数据库中保存，
	{
		name : php字典
		count:个数
		excerpt :描述
		contents : {
			'dict1','dict2','dict3'
		}
	}
 */
class dict {
	private $mongo = null;
	private $db = null;
	private $collection = null;
	private $id = null;
	private $error_info = '';
	private $dict = '';


	/**
	 * [CreateDict 根据条件创建字典]
	 * @param [type] $information [description]
	 */
	public function CreateDict($information){

	}

	/**
	 * [Export 导出字典中所有数据]
	 */
	public function Export(){

	}

	/**
	 * [按condtion指定的条件向字典中加入一个数据]
	 * @param [type] $info      [description]
	 * @param [type] $condition [description]
	 */
	public function Insert($info,$condition){

	}

	/**
	 * [删除字典中一个数据]
	 * @param [type] $info      [description]
	 */
	public function Delete($info){

	}

	/**
	 * 按condtion的条件向字典中导入文件
	 * @param [type] $file      [description]
	 * @param [type] $condition [description]
	 */
	public function Import($file, $condition) {

	}



	public getLastError(){
		return $this->error_info;
	}

	public function __construct($dict='',$type='open',){
		try {
			$this->mongo = new MongoClient(DBCOON,array("persist" => "x"));
			$this->db = $this->mongo->selectDB(DICTDB);
			$this->collection = $this->db->selectCollection(COLLEC);
			$this->dict = $dict;
			$row = $this->collection->finOne(array('name'=>$dict));
			if (!$row) {
				if ($type == 'open') {
					$this->error_info = '字典文件不存在';
					return false;
				} else {
					$this->collection->insert(array('name'=>$dict));
				}
			} else {
				if ($type == 'create') {
					$this->error_info = '字典文件已存在';
					return false;
				}
			}
			$this->collection->finOne(array('name'=>$dict));
			$this->id = $row['_id'];
			
		} catch(MongoConnectionException $e) {
			$this->error_info = $e->getMessage();
			return null;
		}
		return true;
	}

	public function __destruct(){
		$this->collection = null;
		$this->db = null;
		$this->mongo = null;
	}
}


?>